package top.lingkang.authoritypro.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.lingkang.authoritypro.data.dao.MenuDao;
import top.lingkang.authoritypro.data.dto.ResponseResult;
import top.lingkang.authoritypro.data.entity.Menu;
import top.lingkang.authoritypro.data.vo.MenuVo;
import top.lingkang.authoritypro.service.MenuService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lingkang
 * Created by 2021/12/13
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private MenuService menuService;

    @GetMapping("/admin-menu")
    public String adminMenu() {
        return "page/admin-menu";
    }

    @GetMapping("list")
    @ResponseBody
    public Object list() {
        return new ResponseResult<>().success(menuDao.getMenuList());
    }

    @Transactional
    @PostMapping("update/status")
    @ResponseBody
    public Object updateStatus(String code, Boolean status) {
        menuDao.updateStatus(code, status ? 1 : 0);
        return new ResponseResult<>().success("更新状态成功！");
    }

    @GetMapping("add")
    public String add(HttpServletRequest request) {
        Menu parentMenu = menuDao.findByCode(request.getParameter("code"));
        Assert.notNull(parentMenu, "不存在父级菜单");
        request.setAttribute("code", parentMenu.getCode());
        request.setAttribute("title", parentMenu.getTitle());
        return "page/menu-edit";
    }

    @PostMapping("add")
    @ResponseBody
    public Object addPost(MenuVo menuVo) {
        Assert.notNull(menuVo, "入参不能为空！");
        Assert.notEmpty(menuVo.getCode(), "菜单编码不能为空");
        Assert.notEmpty(menuVo.getParentCode(), "菜单父级编码不能为空");
        Assert.notEmpty(menuVo.getIcon(), "菜单图标不能为空");
        Assert.notEmpty(menuVo.getTitle(), "菜单标题不能为空");
        Assert.notEmpty(menuVo.getUrl(), "菜单URL不能为空");
        Assert.isFalse(menuVo.getSeq() == null, "菜单排序不能为空");
        menuService.addMenu(menuVo);
        return new ResponseResult<>().success("添加菜单成功");
    }

    @GetMapping("edit")
    public String edit(HttpServletRequest request) {
        request.setAttribute("status", "edit");
        String code = request.getParameter("code");
        Assert.notEmpty(code, "入参code不能为空！");
        Menu menu = menuDao.findByCode(code);
        Assert.notNull(menu, "不存在的菜单");
        request.setAttribute("menu", menu);

        Menu parentMenu = menuDao.findByCode(menu.getParentCode());
        Assert.notNull(parentMenu, "不存在父级菜单");
        request.setAttribute("code", parentMenu.getCode());
        request.setAttribute("title", parentMenu.getTitle());
        return "page/menu-edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public Object editPost(MenuVo menuVo) {
        Assert.notNull(menuVo, "入参不能为空！");
        Assert.notEmpty(menuVo.getCode(), "菜单编码不能为空");
        Assert.notEmpty(menuVo.getParentCode(), "菜单父级编码不能为空");
        Assert.notEmpty(menuVo.getIcon(), "菜单图标不能为空");
        Assert.notEmpty(menuVo.getTitle(), "菜单标题不能为空");
        Assert.notEmpty(menuVo.getUrl(), "菜单URL不能为空");
        Assert.isFalse(menuVo.getSeq() == null, "菜单排序不能为空");
        Assert.isFalse(menuVo.getId() == null, "菜单Id不能为空");
        menuService.editMenu(menuVo);
        return new ResponseResult<>().success("编辑菜单成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public Object delete(String code) {
        Assert.notEmpty(code, "入参code不能为空！");
        int res = menuService.deleteMenu(code);
        return new ResponseResult<>().success(String.format("删除成功！共删除 %d 个菜单", res));
    }

    @PostMapping("batchDelete")
    @ResponseBody
    public Object batchDelete(String codes) {
        Assert.notNull(codes, "入参 codes 不能为空！");
        List<String> deleteCodes = new ArrayList<>();
        for (String code : codes.split(",")) {
            if (StrUtil.isNotBlank(code))
                deleteCodes.add(code);
        }
        int res = menuService.batchDelete(deleteCodes);
        return new ResponseResult<>().success(String.format("删除成功！共删除 %d 个菜单", res));
    }

    @GetMapping("addParentMneu")
    public String addParentMneu() {
        return "page/menu-edit-parent";
    }

    @GetMapping("editParentMneu")
    public String editParentMneu(String code, HttpServletRequest request) {
        Assert.notNull(code, "入参 codes 不能为空！");
        Menu menu = menuDao.findByCode(code);
        request.setAttribute("menu", menu);
        return "page/menu-edit-parent";
    }

    @PostMapping("addParentMneu")
    @ResponseBody
    public Object addParentMneuPost(MenuVo menuVo) {
        Assert.notNull(menuVo, "入参不能为空！");
        Assert.notEmpty(menuVo.getCode(), "菜单编码不能为空");
        Assert.notEmpty(menuVo.getIcon(), "菜单图标不能为空");
        Assert.notEmpty(menuVo.getTitle(), "菜单标题不能为空");
        Assert.isFalse(menuVo.getSeq() == null, "菜单排序不能为空");

        menuService.addParentMneu(menuVo);
        return new ResponseResult<>().success("添加菜单成功！");
    }

    @PostMapping("editParentMneu")
    @ResponseBody
    public Object editParentMneuPost(MenuVo menuVo) {
        Assert.notNull(menuVo, "入参不能为空！");
        Assert.notEmpty(menuVo.getIcon(), "菜单图标不能为空");
        Assert.notEmpty(menuVo.getTitle(), "菜单标题不能为空");
        Assert.isFalse(menuVo.getSeq() == null, "菜单排序不能为空");

        menuService.editParentMneu(menuVo);
        return new ResponseResult<>().success("编辑菜单成功");
    }
}
